var ScrambledEggs;
(function (ScrambledEggs) {
    (function (PartTimeJob) {
        PartTimeJob._map = [];
        PartTimeJob._map[0] = "None";
        PartTimeJob.None = 0;
        PartTimeJob._map[1] = "Manager";
        PartTimeJob.Manager = 1;
        PartTimeJob._map[2] = "Cleaning";
        PartTimeJob.Cleaning = 2;
        PartTimeJob._map[3] = "Shoemaker";
        PartTimeJob.Shoemaker = 3;
        PartTimeJob._map[4] = "Banker";
        PartTimeJob.Banker = 4;
        PartTimeJob._map[5] = "Research";
        PartTimeJob.Research = 5;
    })(ScrambledEggs.PartTimeJob || (ScrambledEggs.PartTimeJob = {}));
    var PartTimeJob = ScrambledEggs.PartTimeJob;
    (function (CatFoodDecision) {
        CatFoodDecision._map = [];
        CatFoodDecision._map[0] = "None";
        CatFoodDecision.None = 0;
        CatFoodDecision._map[1] = "Reached";
        CatFoodDecision.Reached = 1;
        CatFoodDecision._map[2] = "Ate";
        CatFoodDecision.Ate = 2;
        CatFoodDecision._map[3] = "Threw";
        CatFoodDecision.Threw = 3;
    })(ScrambledEggs.CatFoodDecision || (ScrambledEggs.CatFoodDecision = {}));
    var CatFoodDecision = ScrambledEggs.CatFoodDecision;
    (function (BabyFinaleDecision) {
        BabyFinaleDecision._map = [];
        BabyFinaleDecision._map[0] = "None";
        BabyFinaleDecision.None = 0;
        BabyFinaleDecision._map[1] = "Poopcat";
        BabyFinaleDecision.Poopcat = 1;
        BabyFinaleDecision._map[2] = "Doorbang";
        BabyFinaleDecision.Doorbang = 2;
        BabyFinaleDecision._map[3] = "Petcat";
        BabyFinaleDecision.Petcat = 3;
        BabyFinaleDecision._map[4] = "Jailbreak";
        BabyFinaleDecision.Jailbreak = 4;
    })(ScrambledEggs.BabyFinaleDecision || (ScrambledEggs.BabyFinaleDecision = {}));
    var BabyFinaleDecision = ScrambledEggs.BabyFinaleDecision;
    (function (GamePhase) {
        GamePhase._map = [];
        GamePhase._map[0] = "Fetus";
        GamePhase.Fetus = 0;
        GamePhase._map[1] = "Baby";
        GamePhase.Baby = 1;
        GamePhase._map[2] = "YoungAdult";
        GamePhase.YoungAdult = 2;
    })(ScrambledEggs.GamePhase || (ScrambledEggs.GamePhase = {}));
    var GamePhase = ScrambledEggs.GamePhase;
    (function (Gender) {
        Gender._map = [];
        Gender._map[0] = "Unknown";
        Gender.Unknown = 0;
        Gender._map[1] = "Male";
        Gender.Male = 1;
        Gender._map[2] = "Female";
        Gender.Female = 2;
    })(ScrambledEggs.Gender || (ScrambledEggs.Gender = {}));
    var Gender = ScrambledEggs.Gender;
    var GameStateProvider = (function () {
        function GameStateProvider(key) {
            this.Key = key;
            this.Settings = $("#settings").data("settings");
        }
        GameStateProvider.DefaultState = {
            Phase: GamePhase.Fetus,
            Gender: Gender.Unknown,
            Message: null,
            PhysicalMaturity: -1,
            MentalMaturity: -1,
            Popularity: -1,
            Intelligence: -1,
            Time: -1,
            Energy: -1,
            Money: -1,
            Name: null,
            Fullness: -1,
            JobCooldown: 0,
            LeftPen: false,
            DecisionCatFood: CatFoodDecision.None,
            DecisionBabyFinale: BabyFinaleDecision.None,
            Hat: "",
            PartTimeJob: PartTimeJob.None,
            JobLevel: 0,
            Cooldowns: {
            },
            HintsOn: true,
            Temporary: {
            },
            Inventory: {
            },
            Version: 14
        };
        GameStateProvider.prototype.load = function (callback) {
            var me = this;
            $.ajax({
                url: "/Api/Saves",
                method: "GET",
                data: {
                    key: this.Key
                }
            }).done(function (data) {
                me.State = JSON.parse(data);
                console.log("state loaded");
                console.log(me.State);
                me.upgrade();
                callback();
            }).fail(function () {
                me.State = $.extend(GameStateProvider.DefaultState, {
                });
                console.log("loaded default state");
                console.log(me.State);
                callback();
            });
        };
        GameStateProvider.prototype.save = function (callback) {
            var me = this;
            $.ajax({
                url: "/Api/Saves",
                method: "PUT",
                data: {
                    key: me.Key,
                    state: JSON.stringify(me.State)
                }
            }).done(function () {
                console.log("state saved to ?key=" + me.Key);
                console.log(me.State);
                callback();
            });
        };
        GameStateProvider.prototype.upgrade = function () {
            if(this.State.Version == GameStateProvider.DefaultState.Version) {
                console.log("loaded version is up to date");
                return;
            }
            switch(this.State.Version) {
                case null:
                case 0:
                    throw "Could not upgrade from unknown version";
                case 1:
                    console.log("upgrading version from 1 to 2");
                    this.State.Message = null;
                    console.log(this.State);
                case 2:
                    console.log("upgrading version from 2 to 3");
                    (this.State).Physique = (GameStateProvider.DefaultState).Physique;
                    (this.State).Intelligence = (GameStateProvider.DefaultState).Intelligence;
                    console.log(this.State);
                case 3:
                    console.log("upgrading version from 3 to 4");
                    this.State.Name = prompt("Missing data: Name; Please update", "King Flar");
                    ;
                    console.log(this.State);
                case 4:
                    console.log("upgrading version from 4 to 5");
                    this.State.Fullness = GameStateProvider.DefaultState.Fullness;
                    console.log(this.State);
                case 5:
                    console.log("upgrading version from 5 to 6");
                    this.State.Cooldowns = {
                    };
                    this.State.Temporary = {
                    };
                    console.log(this.State);
                case 6:
                    console.log("upgrading version from 6 to 7");
                    this.State.Inventory = {
                    };
                    this.State.LeftPen = false;
                    console.log(this.State);
                case 7:
                    console.log("upgrading version from 7 to 8");
                    (this.State).CatFood = 0;
                    console.log(this.State);
                case 8:
                    console.log("upgrading version from 8 to 9");
                    this.State.DecisionCatFood = (this.State).CatFood;
                    this.State.DecisionBabyFinale = BabyFinaleDecision.None;
                    console.log(this.State);
                case 9:
                    console.log("upgrading version from 9 to 10");
                    this.State.MentalMaturity = (this.State).Physique;
                    this.State.PhysicalMaturity = (this.State).Intelligence;
                    delete (this.State).Physique;
                    delete (this.State).Intelligence;
                    this.State.Popularity = GameStateProvider.DefaultState.Popularity;
                    this.State.Intelligence = GameStateProvider.DefaultState.Intelligence;
                    this.State.Time = GameStateProvider.DefaultState.Time;
                    this.State.Energy = GameStateProvider.DefaultState.Energy;
                    this.State.Money = GameStateProvider.DefaultState.Money;
                    console.log(this.State);
                case 10:
                    console.log("upgrading version from 10 to 11");
                    this.State.PartTimeJob = GameStateProvider.DefaultState.PartTimeJob;
                    console.log(this.State);
                case 11:
                    console.log("upgrading version from 11 to 12");
                    this.State.JobCooldown = GameStateProvider.DefaultState.JobCooldown;
                    console.log(this.State);
                case 12:
                    console.log("upgrading version from 12 to 13");
                    this.State.HintsOn = GameStateProvider.DefaultState.HintsOn;
                    console.log(this.State);
                case 13:
                    console.log("upgrading version from 13 to 14");
                    this.State.Hat = GameStateProvider.DefaultState.Hat;
                    console.log(this.State);
                case 14:
                    console.log("upgrading version from 14 to 15");
                    this.State.JobLevel = GameStateProvider.DefaultState.JobLevel;
                    console.log(this.State);
            }
            this.State.Version = GameStateProvider.DefaultState.Version;
        };
        return GameStateProvider;
    })();
    ScrambledEggs.GameStateProvider = GameStateProvider;    
})(ScrambledEggs || (ScrambledEggs = {}));
